﻿#region File and License Information
/*
<File>
	<License>
		Copyright © 2009 - 2017, Daniel Vaughan. All rights reserved.
		This file is part of Calcium (http://calciumsdk.net), 
		which is released under the MIT License.
		See file /Documentation/License.txt for details.
	</License>
	<CreationDate>2012-02-18 13:17:48Z</CreationDate>
</File>
*/
#endregion

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Outcoder.Linq
{
	/// <summary>
	/// This class is used to group a set of objects within, for example, a LongListSelector.
	/// </summary>
	/// <typeparam name="TElement">The type of the element 
	/// that is group by an object key.</typeparam>
	[Obsolete("Please use GroupedList instead.")]
	public class Grouping<TElement> : Grouping<object, TElement>
	{
		public Grouping(object key, IEnumerable<TElement> items)
			: base(ArgumentValidator.AssertNotNull(key, "key"), items)
		{
			/* Intentionally left blank. */
		}
	}

	/// <summary>
	/// This class is used to group a set of objects within, for example, a LongListSelector.
	/// </summary>
	/// <typeparam name="TElement">The type of the element 
	/// that is group by an object key.</typeparam>
	/// <typeparam name="TKey">The type of the key object.</typeparam>
	[Obsolete("Please use GroupedList instead.")]
	public class Grouping<TKey, TElement> : IGrouping<TKey, TElement>
	{
		readonly IEnumerable<TElement> items;
		readonly TKey key;

		public Grouping(TKey key, IEnumerable<TElement> items)
		{
			this.key = key;
			this.items = ArgumentValidator.AssertNotNull(items, "items");
		}

		public TKey Key
		{
			get
			{
				return key;
			}
		}

		public IEnumerator<TElement> GetEnumerator()
		{
			return items.GetEnumerator();
		}

		IEnumerator IEnumerable.GetEnumerator()
		{
			return items.GetEnumerator();
		}

		public bool HasItems
		{
			get
			{
				return items.Any();
			}
		}

		public override bool Equals(object obj)
		{
			var otherGrouping = obj as Grouping<TKey, TElement>;
			return otherGrouping != null && Key.Equals(otherGrouping.Key);
		}

		public override int GetHashCode()
		{
			return Key.GetHashCode();
		}
	}
}